package com.example.homework3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.homework3.ChoiceDialogFragment;
import com.example.homework3.DiyDialogFragment;
import com.example.homework3.MuchChoiceDialogFragment;
import com.example.homework3.SimpleDialogFragment;
import com.example.homework3.SingleDialogFragment;

public class MainActivity extends AppCompatActivity {
    private Button mBtnOldTalk;
    private Button mBtnSimpleTalk;
    private Button mBtnNormalChoiceTalk;
    private Button mBtnSingleChoiceTalk;
    private Button mBtnMuchChoiceTalk;
    private Button mBtnDiyTalk;
    private AlertDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initEvent();
    }

    private void initEvent() {
        mBtnOldTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //旧版显示对话框
                dialog = new AlertDialog.Builder(MainActivity.this).setTitle("旧版对话框")
                        .setMessage("这是旧版的对话框，使用简单\n但是没有自动支持屏幕翻转！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你确定了！", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "你取消了！", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                dialog.show();
            }
        });

        mBtnSimpleTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDialogFragment dialog = new SimpleDialogFragment();
                dialog.show(getSupportFragmentManager(), "simple");
            }
        });

        mBtnNormalChoiceTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoiceDialogFragment dialog = new ChoiceDialogFragment();
                dialog.setListener(new ChoiceDialogFragment.NotifyDialogListener() {
                    @Override
                    public void onDialogPositiveClicked(String gender) {
                        Toast.makeText(getApplicationContext(), "你选择了" + gender, Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show(getSupportFragmentManager(), "choice");
            }
        });

        mBtnSingleChoiceTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SingleDialogFragment dialog = new SingleDialogFragment();
                dialog.show(getSupportFragmentManager(), "SingleChoice");
            }
        });

        mBtnMuchChoiceTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MuchChoiceDialogFragment dialog = new MuchChoiceDialogFragment();
                dialog.show(getSupportFragmentManager(), "MuchChoice");
            }
        });

        mBtnDiyTalk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiyDialogFragment dialog = new DiyDialogFragment();
                dialog.setListener(new DiyDialogFragment.NotifyDialogListener() {
                    @Override
                    public void onDialogPositiveClicked(String gender1, String gender2) {
                        Toast.makeText(getApplicationContext(), "你输入了" + gender1 + "和" + gender2, Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show(getSupportFragmentManager(), "Diy");
            }
        });
    }

    private void initView() {
        mBtnOldTalk = findViewById(R.id.up);
        mBtnSimpleTalk = findViewById(R.id.btn_simple_talk);
        mBtnNormalChoiceTalk = findViewById(R.id.btn_normal_choice_talk);
        mBtnSingleChoiceTalk = findViewById(R.id.btn_single_choice_talk);
        mBtnMuchChoiceTalk = findViewById(R.id.btn_much_choice_talk);
        mBtnDiyTalk = findViewById(R.id.btn_diy_talk);
    }
}